package com.woniuxy.charin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.model.entity.TransactionOrder;
import com.woniuxy.charin.model.vo.TransactionOrderVo;
import com.woniuxy.charin.service.TransactionOrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Api(tags = "交易管理")
@RestController
@RequestMapping("/transactionOrders")
public class TransactionOrderController {
    @Autowired
    private TransactionOrderService transactionOrderService;

    @PostMapping("/getTransactionOrders")
    public ResponseEntity<Map<String, Object>> getTransactionOrders(@RequestBody Map<String, Object> params) {
        // 提取分页参数，设置默认值
        int pageNum = params.get("pageNum") != null ? Integer.parseInt(params.get("pageNum").toString()) : 1;
        int pageSize = params.get("pageSize") != null ? Integer.parseInt(params.get("pageSize").toString()) : 10;

        // 创建查询参数 Map（仅保留非空参数）
        Map<String, Object> queryParams = new HashMap<>();
        if (params.get("orderNo") != null && !params.get("orderNo").toString().isEmpty()) {
            queryParams.put("orderNo", params.get("orderNo").toString());
        }
        if (params.get("customerName") != null && !params.get("customerName").toString().isEmpty()) {
            queryParams.put("customerName", params.get("customerName").toString());
        }
        if (params.get("phoneNumber") != null && !params.get("phoneNumber").toString().isEmpty()) {
            queryParams.put("phoneNumber", params.get("phoneNumber").toString());
        }
        if (params.get("transactionType") != null) {
            queryParams.put("transactionType", params.get("transactionType"));
        }
        if (params.get("transactionStatus") != null && !params.get("transactionStatus").toString().isEmpty()) {
            queryParams.put("transactionStatus", params.get("transactionStatus").toString());
        }

        // 调用服务层进行查询
        Page<TransactionOrderVo> transactionOrders = transactionOrderService.getTransactionOrders(queryParams, pageNum, pageSize);

        // 封装返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("records", transactionOrders.getRecords());
        result.put("total", transactionOrders.getTotal());
        result.put("current", transactionOrders.getCurrent());
        result.put("size", transactionOrders.getSize());

        return ResponseEntity.ok(result);
    }

    @GetMapping("/findTransactionOrderById/{id}")
    public TransactionOrderVo getTransactionOrderById(@PathVariable("id") Long id) {
        return transactionOrderService.getTransactionOrderById(id);//5
    }

    @PostMapping("/addTransactionOrder")
    public ResponseEntity<String> addTransactionOrder(@RequestBody TransactionOrder transactionOrder) {
        transactionOrderService.addTransactionOrder(transactionOrder);
        return ResponseEntity.ok("Transaction Order added successfully");
    }


}
